package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.PigstyDAO;
import cn.edu.scau.cmi.hombio.dao.PreventionDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;

import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.Pigsty;
import cn.edu.scau.cmi.hombio.domain.Prevention;
import cn.edu.scau.cmi.hombio.domain.Task;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Prevention entities
 * 
 */

@Service("PreventionService")

@Transactional
public class PreventionServiceImpl implements PreventionService {

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages Pigsty entities
	 * 
	 */
	@Autowired
	private PigstyDAO pigstyDAO;

	/**
	 * DAO injected by Spring that manages Prevention entities
	 * 
	 */
	@Autowired
	private PreventionDAO preventionDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * Instantiates a new PreventionServiceImpl.
	 *
	 */
	public PreventionServiceImpl() {
	}

	/**
	 * Save an existing Prevention entity
	 * 
	 */
	@Transactional
	public void savePrevention(Prevention prevention) {
		Prevention existingPrevention = preventionDAO.findPreventionByPrimaryKey(prevention.getId());

		if (existingPrevention != null) {
			if (existingPrevention != prevention) {
				existingPrevention.setId(prevention.getId());
				existingPrevention.setTime(prevention.getTime());
				existingPrevention.setMemo(prevention.getMemo());
			}
			prevention = preventionDAO.store(existingPrevention);
		} else {
			prevention = preventionDAO.store(prevention);
		}
		preventionDAO.flush();
	}

	/**
	 * Delete an existing Employee entity
	 * 
	 */
	@Transactional
	public Prevention deletePreventionEmployee(Integer prevention_id, Integer related_employee_id) {
		Prevention prevention = preventionDAO.findPreventionByPrimaryKey(prevention_id, -1, -1);
		Employee related_employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		prevention.setEmployee(null);
		related_employee.getPreventions().remove(prevention);
		prevention = preventionDAO.store(prevention);
		preventionDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		employeeDAO.remove(related_employee);
		employeeDAO.flush();

		return prevention;
	}

	/**
	 * Save an existing Pigsty entity
	 * 
	 */
	@Transactional
	public Prevention savePreventionPigsty(Integer id, Pigsty related_pigsty) {
		Prevention prevention = preventionDAO.findPreventionByPrimaryKey(id, -1, -1);
		Pigsty existingpigsty = pigstyDAO.findPigstyByPrimaryKey(related_pigsty.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigsty != null) {
			existingpigsty.setId(related_pigsty.getId());
			existingpigsty.setNumber(related_pigsty.getNumber());
			existingpigsty.setRfid(related_pigsty.getRfid());
			existingpigsty.setCapacity(related_pigsty.getCapacity());
			existingpigsty.setMemo(related_pigsty.getMemo());
			related_pigsty = existingpigsty;
		}

		prevention.setPigsty(related_pigsty);
		related_pigsty.getPreventions().add(prevention);
		prevention = preventionDAO.store(prevention);
		preventionDAO.flush();

		related_pigsty = pigstyDAO.store(related_pigsty);
		pigstyDAO.flush();

		return prevention;
	}

	/**
	 */
	@Transactional
	public Prevention findPreventionByPrimaryKey(Integer id) {
		return preventionDAO.findPreventionByPrimaryKey(id);
	}

	/**
	 * Delete an existing Task entity
	 * 
	 */
	@Transactional
	public Prevention deletePreventionTask(Integer prevention_id, Integer related_task_id) {
		Prevention prevention = preventionDAO.findPreventionByPrimaryKey(prevention_id, -1, -1);
		Task related_task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		prevention.setTask(null);
		related_task.getPreventions().remove(prevention);
		prevention = preventionDAO.store(prevention);
		preventionDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		taskDAO.remove(related_task);
		taskDAO.flush();

		return prevention;
	}

	/**
	 * Delete an existing Pigsty entity
	 * 
	 */
	@Transactional
	public Prevention deletePreventionPigsty(Integer prevention_id, Integer related_pigsty_id) {
		Prevention prevention = preventionDAO.findPreventionByPrimaryKey(prevention_id, -1, -1);
		Pigsty related_pigsty = pigstyDAO.findPigstyByPrimaryKey(related_pigsty_id, -1, -1);

		prevention.setPigsty(null);
		related_pigsty.getPreventions().remove(prevention);
		prevention = preventionDAO.store(prevention);
		preventionDAO.flush();

		related_pigsty = pigstyDAO.store(related_pigsty);
		pigstyDAO.flush();

		pigstyDAO.remove(related_pigsty);
		pigstyDAO.flush();

		return prevention;
	}

	/**
	 * Save an existing Task entity
	 * 
	 */
	@Transactional
	public Prevention savePreventionTask(Integer id, Task related_task) {
		Prevention prevention = preventionDAO.findPreventionByPrimaryKey(id, -1, -1);
		Task existingtask = taskDAO.findTaskByPrimaryKey(related_task.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtask != null) {
			existingtask.setId(related_task.getId());
			existingtask.setTaskNumber(related_task.getTaskNumber());
			existingtask.setCreateTime(related_task.getCreateTime());
			existingtask.setStartTime(related_task.getStartTime());
			existingtask.setEndTime(related_task.getEndTime());
			existingtask.setMemo(related_task.getMemo());
			related_task = existingtask;
		}

		prevention.setTask(related_task);
		related_task.getPreventions().add(prevention);
		prevention = preventionDAO.store(prevention);
		preventionDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		return prevention;
	}

	/**
	 * Delete an existing Prevention entity
	 * 
	 */
	@Transactional
	public void deletePrevention(Prevention prevention) {
		preventionDAO.remove(prevention);
		preventionDAO.flush();
	}

	/**
	 * Return all Prevention entity
	 * 
	 */
	@Transactional
	public List<Prevention> findAllPreventions(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Prevention>(preventionDAO.findAllPreventions(startResult, maxRows));
	}

	/**
	 * Load an existing Prevention entity
	 * 
	 */
	@Transactional
	public Set<Prevention> loadPreventions() {
		return preventionDAO.findAllPreventions();
	}

	/**
	 * Return a count of all Prevention entity
	 * 
	 */
	@Transactional
	public Integer countPreventions() {
		return ((Long) preventionDAO.createQuerySingleResult("select count(o) from Prevention o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing Employee entity
	 * 
	 */
	@Transactional
	public Prevention savePreventionEmployee(Integer id, Employee related_employee) {
		Prevention prevention = preventionDAO.findPreventionByPrimaryKey(id, -1, -1);
		Employee existingemployee = employeeDAO.findEmployeeByPrimaryKey(related_employee.getId());

		// copy into the existing record to preserve existing relationships
		if (existingemployee != null) {
			existingemployee.setId(related_employee.getId());
			existingemployee.setNumber(related_employee.getNumber());
			existingemployee.setName(related_employee.getName());
			existingemployee.setPassword(related_employee.getPassword());
			existingemployee.setLogOn(related_employee.getLogOn());
			existingemployee.setRfid(related_employee.getRfid());
			existingemployee.setSex(related_employee.getSex());
			existingemployee.setBirthday(related_employee.getBirthday());
			existingemployee.setState(related_employee.getState());
			existingemployee.setMemo(related_employee.getMemo());
			related_employee = existingemployee;
		}

		prevention.setEmployee(related_employee);
		related_employee.getPreventions().add(prevention);
		prevention = preventionDAO.store(prevention);
		preventionDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		return prevention;
	}
}
